import { toBase64String, fromBase64String } from '@web-utils/core'

export const iframePath = 'iframe'

/**
 * 是不是要在应用内打开的链接 /iframe/hash
 * @param path
 * @returns {boolean}
 */
export const isIframeRoute = path => path.indexOf(`/${iframePath}/`) > -1

/**
 * 是不是需要在浏览器新标签打开链接
 * @param path
 * @returns {boolean}
 */
export const isNewTabPath = path => /^http(s)?:\/\/.*\?.*tab=new.*/.test(path)

// 表单路径,例如: abc/:id
const formPathReg = /\/:.*/

export const isFormPath = path => formPathReg.test(path)

export const fixPath = path => path.replace(/^\//, '').replace(formPathReg, '')

export const urlToHash = url => toBase64String(url)

export const hashToUrl = hash => fromBase64String(hash)

export const getUrlHash = path => path.replace(`/${iframePath}/`, '')

/**
 * 从路径中获取 url
 * @param route
 * @returns {string|null}
 */
export const getUrlFromPath = route => isIframeRoute(route.path) ? hashToUrl(route.params.hash) : null

